package com.bjx.flow.mmx.task.node;

import cn.hutool.core.collection.ListUtil;
import com.bjx.common.core.util.SpringUtil;
import com.bjx.flow.mmx.bean.MmxFlowData;
import com.bjx.flow.mmx.bean.MmxNodeData;
import com.bjx.flow.mmx.constant.MmxNodeConstant;
import com.bjx.flow.mmx.domain.bo.MmxNodeFlowBO;
import com.bjx.flow.mmx.task.MmxAbstractNodeFlowDispose;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @desc: 用户任务节点
 * @author: liushuai
 * @date: 2023/1/31 15:17
 */
@Service(MmxNodeConstant.USER_TASK)
public class MmxUserNodeFlowDispose extends MmxAbstractNodeFlowDispose {
    @Override
    public MmxNodeFlowBO execute(MmxFlowData flowObj, MmxNodeData currentNode, Map<String, Object> variables) {
        List<MmxNodeData> targerNode = this.getTargerNode(flowObj, currentNode);
        if(targerNode.size() == 0){
            throw new RuntimeException("目标节点不存在");
        }
        if(targerNode.size() > 1){
            throw new RuntimeException("目标节点只能存在一个");
        }
        MmxNodeData nodeData = targerNode.get(0);
        if(!TRANSIT_NODE.contains(nodeData.getType())){
            return MmxNodeFlowBO.builder()
                    .nodeType(MmxNodeConstant.PARALLEL_GATEWAY)
                    .nodeData(ListUtil.list(false,nodeData))
                    .build();
        }
        MmxAbstractNodeFlowDispose taskNode = SpringUtil.getBean(nodeData.getType());
        MmxNodeFlowBO nodeFlowBO = taskNode.execute(flowObj, nodeData,variables);
        return nodeFlowBO;
    }
}
